﻿var noticeStateJson;
var noticeTypeJson;
var GetPrizeNameJson;

$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        datatype: "json",
        url: "../../Ajax/ClassModeAjax.ashx",
        beforSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = $.parseJSON(data).json;
                var tbody = "";
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                    tbody += v.Name;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblCreateTime'>";
                    tbody += v.CreateTime;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblStartTime'>";
                    tbody += v.StartTime;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblEndTime'>";
                    tbody += v.EndTime;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblNoticeContent'>";
                    tbody += decodeURIComponent(v.NoticeContent);
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblState' id='" + v.State + "'>";
                    tbody += v.StateName;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblTypeID' id='" + v.TypeID + "'>";
                    tbody += v.TypeName;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblR'>";
                    tbody += v.R;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblG'>";
                    tbody += v.G;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblB'>";
                    tbody += v.B;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody)
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    $.ajax({
        type: "post",
        data: { action: "GetPrizeNameByPrizeTypeID", typeID: 4 },
        dataType: "json",
        url: "../../Ajax/PrizeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                GetPrizeNameJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })

    $.ajax({
        type: "post",
        data: { action: "GetNoticeState" },
        dataType: "json",
        url: "../../Ajax/NoticeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                noticeStateJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    $.ajax({
        type: "post",
        data: { action: "GetNoticeType" },
        dataType: "json",
        url: "../../Ajax/NoticeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                noticeTypeJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    $("#btnAdd").click(function () {
        Edit(null);
    })

    //手动添加用户公告 
    $("#btnHandAdd").click(function () {
        var content = "";
        content += "<table>";
        content += "<tr><td>公告类型：<select id='selType' ><option value='1' selected='selected'>充值公告</option><option value='2'>兑奖公告</option><option value='4'>彩池模式彩蛋公告</option></select></td></tr>";
        content += "<tr><td>系统账号：<input type='text' id='txtUserAccount'  style='width:200px' /></td></tr>"
        content += "<tr><td>物品名称：<select id='selPrize' >" + InitSelectPrizeName() + "</select></td></tr>"
        content += "<tr><td>物品数量：<input type='text' id='txtPrizeNum'  style='width:200px' /></td></tr>"
        content += "<tr><td>物品价格：<input type='text' id='txtPrizePrice'  style='width:200px' /></td></tr>"
        content += "</table>";
        $.dialog({
            title: "经典公告管理设置模板",
            content: content,
            button: [
                    {
                        name: "提交",
                        callback: function () {
                            var dynameicType = $("#selType").val();
                            var userAccount = $("#txtUserAccount").val();
                            var prizeName = $("#selPrize>option:selected").text();
                            var prizeNum = $("#txtPrizeNum").val();
                            var prizePrice = $("#txtPrizePrice").val();
                           
                            if (dynameicType == "" || userAccount == "" || prizeName == "" || prizeNum == "" || prizePrice == "") {
                                $.dialog.alert("请正确填写参数，不可为空！");
                                return;
                            }
                            else {
                                $.ajax({
                                    type: "post",
                                    data: { action: "AddHandNotice", dynameicType: dynameicType, userAccount: userAccount, prizeName: prizeName, prizeNum: prizeNum, prizePrice: prizePrice },
                                    async: true,
                                    url: "../../Ajax/ClassModeAjax.ashx",
                                    beforeSend: function () {
                                    },
                                    success: function (data) {
                                        $.dialog.tips("更新成功！", 1);
                                    },
                                    complete: function () {
                                    },
                                    error: function () {
                                    }
                                })
                            }
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
            lock: true,
            drag: false
        });
    })
})

function InitSelectNoticeState(value) {
    var sel = "";
    $.each(noticeStateJson, function (k, v) {
        if ((value == null && k == 0) || v.value == value)
            sel += "<option value='" + v.value + "' selected='selected'>" + v.name + "</option>";
        else
            sel += "<option value='" + v.value + "'>" + v.name + "</option>";
    })
    return sel;
}
function InitSelectNoticeType(value) {
    var sel = "";
    $.each(noticeTypeJson, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}
function InitSelectPrizeName(value) {
    var sel = "";
    $.each(GetPrizeNameJson, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}

function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    Edit(lbl);
}
function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var sortNum = $(lbl).parent().parent().find("lable[name=lblName]");
    var sortId = $(lbl).parent().parent().find("lable[name=lblName]").attr("id");
    var stateName = $(lbl).parent().parent().find("lable[name=lblState]").text();
    var state = $(lbl).parent().parent().find("lable[name=lblState]").attr("id")
    var TypeName = $(lbl).parent().parent().find("lable[name=lblTypeID]").text();
    var TypeID = $(lbl).parent().parent().find("lable[name=lblTypeID]").attr("id");
    var content = "";
    var data = new Date();
    if (lbl != null) {
        content = "<table><tr><td>公告名称：<input type='text' id='txtName' value='" + $(lbl).parent().parent().find("lable[name=lblName]").first().text() + "' style='width:200px' /></td></tr>"
        content += "<tr><td>开始时间：<input type='text' id='txtStartTime' value='" + $(lbl).parent().parent().find("lable[name=lblStartTime]").text() + "' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\" class=\"Wdate\"  style='width:200px' /></td></tr>"
        content += "<tr><td>结束时间：<input type='text' id='txtEndTime' value='" + $(lbl).parent().parent().find("lable[name=lblEndTime]").text() + "' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\" class=\"Wdate\"  style='width:200px' /></td></tr>"
        content += "<tr><td>公告内容：<textarea  cols='50' rows='5' id='txtNoticeContent' style='width:197px'>" + $(lbl).parent().parent().find("lable[name=lblNoticeContent]").text() + "</textarea></td></tr>"
        content += "<tr><td>公告状态：<select id='selState' >" + InitSelectNoticeState(state) + "</select></td></tr>"
        content += "<tr><td>公告类型：<select id='selTypeID' >" + InitSelectNoticeType(TypeID) + "</select></td></tr>"
        content += "<tr><td>R：<input type='text' id='txtR' value='" + $(lbl).parent().parent().find("lable[name=lblR]").first().text() + "' style='width:200px' /></td></tr>"
        content += "<tr><td>G：<input type='text' id='txtG' value='" + $(lbl).parent().parent().find("lable[name=lblG]").first().text() + "' style='width:200px' /></td></tr>"
        content += "<tr><td>B：<input type='text' id='txtB' value='" + $(lbl).parent().parent().find("lable[name=lblB]").first().text() + "' style='width:200px' /></td></tr></table>"
    }
    else {
        content = "<table><tr><td>公告名称：<input type='text' id='txtName' style='width:200px' /></td></tr>"
        content += "<tr><td>创建时间：<lable name='lblCreateTime'>" + data.getFullYear() + "." + (data.getMonth() + 1) + "." + data.getDate() + "</lable></td></tr>"
        content += "<tr><td>开始时间：<input type='text' id='txtStartTime' style='width:200px' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\" class=\"Wdate\"  /></td></tr>"
        content += "<tr><td>结束时间：<input type='text' id='txtEndTime' style='width:200px' onfocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\" class=\"Wdate\"  /></td></tr>"
        content += "<tr><td>公告内容：<textarea  cols='50' rows='5' id='txtNoticeContent' style='width:197px'></textarea></td></tr>"
        content += "<tr><td>公告状态：<select id='selState' >" + InitSelectNoticeState() + "</select></td></tr>"
        content += "<tr><td>公告类型：<select id='selTypeID' >" + InitSelectNoticeType() + "</select></td></tr>"
        content += "<tr><td>R：<input type='text' id='txtR' style='width:200px' /></td></tr>"
        content += "<tr><td>G：<input type='text' id='txtG' style='width:200px' /></td></tr>"
        content += "<tr><td>B：<input type='text' id='txtB' style='width:200px' /></td></tr></table>"

    }

    $.dialog({
        title: "经典公告管理设置模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var objectName = $("#txtName").val();
                            AddMould(lbl, $(txtName), sortId);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}
function AddMould(contr, sortNum, sortId) {
    var objectId = sortId;
    var objectName = $("#txtName").val();
    var CreateTime = $(sortNum).parent().parent().parent().find("lable[name=lblCreateTime]").text();
    var StartTime = $(sortNum).parent().parent().parent().find("#txtStartTime").val();
    var EndTime = $(sortNum).parent().parent().parent().find("#txtEndTime").val();
    var NoticeContent = $(sortNum).parent().parent().parent().find("#txtNoticeContent").val();
    var State = $(sortNum).parent().parent().parent().find("#selState[option:selected]").val();
    var StateName = $(sortNum).parent().parent().parent().find("#selState>option:selected").text();
    var TypeID = $(sortNum).parent().parent().parent().find("#selTypeID").val();
    var TypeName = $(sortNum).parent().parent().parent().find("#selTypeID>option:selected").text();
    var IsFullScre = false;
    var IsStick = true;
    var IsRoll = true;
    var R = $(sortNum).parent().parent().parent().find("#txtR").val();
    var G = $(sortNum).parent().parent().parent().find("#txtG").val();
    var B = $(sortNum).parent().parent().parent().find("#txtB").val();
    //    var TypeID = $(sortNum).parent().parent().parent().find("#selTypeID[option:selected]").val();
    //    var R = $(sortNum).parent().parent().parent().find("#txtR").val();
    //    var G = $(sortNum).parent().parent().parent().find("#txtG").val();
    //    var B = $(sortNum).parent().parent().parent().find("#txtB").val();
    var data = contr == null ? { action: "Add", name: objectName, CreateTime: CreateTime, StartTime: StartTime, EndTime: EndTime, NoticeContent: NoticeContent, state: State, TypeID: TypeID, IsFullScre: IsFullScre, IsStick: IsStick, IsRoll: IsRoll, R: R, G: G, B: B} :
              { action: "Update", id: objectId, name: objectName, CreateTime: CreateTime, StartTime: StartTime, EndTime: EndTime, NoticeContent: NoticeContent, State: State, TypeID: TypeID, IsFullScre: IsFullScre, IsStick: IsStick, IsRoll: IsRoll, R: R, G: G, B: B };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/ClassModeAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {

            $.dialog.tips("更新成功！", 1);
            if (contr == null) {
                var tbody = "<tr>";
                tbody += "<td><lable name='lblName' id='" + data + "'>";
                tbody += objectName;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblCreateTime'>";
                tbody += CreateTime;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblStartTime'>";
                tbody += StartTime;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblEndTime'>";
                tbody += EndTime;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblNoticeContent'>";
                tbody += NoticeContent;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblState' id='" + State + "'>";
                tbody += StateName;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblTypeID' id='" + TypeID + "'>";
                tbody += TypeName;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblR'>";
                tbody += R;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblG'>";
                tbody += G;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblB'>";
                tbody += B;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                tbody += "</td>";
                tbody += "</td>";
                tbody += "</tr>"
                $("#tb1").append(tbody);
            }
            else {
                $(contr).parent().parent().find("lable[name=lblName]").first().text(objectName);
                $(contr).parent().parent().find("lable[name=lblCreateTime]").first().text(CreateTime);
                $(contr).parent().parent().find("lable[name=lblStartTime]").first().text(StartTime);
                $(contr).parent().parent().find("lable[name=lblEndTime]").first().text(EndTime);
                $(contr).parent().parent().find("lable[name=lblNoticeContent]").first().text(NoticeContent);
                $(contr).parent().parent().find("lable[name=lblState]").first().text(StateName);
                $(contr).parent().parent().find("lable[name=lblState]").attr("id", State);
                $(contr).parent().parent().find("lable[name=lblTypeID]").first().text(TypeName);
                $(contr).parent().parent().find("lable[name=lblTypeID]").attr("id", TypeID);
                $(contr).parent().parent().find("lable[name=lblR]").first().text(R);
                $(contr).parent().parent().find("lable[name=lblG]").first().text(G);
                $(contr).parent().parent().find("lable[name=lblB]").first().text(B);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
function Delete(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: $(lbl).attr("id") },
            async: true,
            url: "../../Ajax/ClassModeAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $($(lbl).parent().parent()).remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}

